<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/14
 * Time: 19:50
 */
namespace app\mtsuperadmin\controller;

use app\mtadminapi\model\MtAdminSysShopExam;
use app\mtsuperadmin\model\MtShopApplyExam;
use app\mtadminapi\model\MtShop;
use app\util\ReturnCode;

/**
 * 合作审核
 * Class ApplyExam
 * @package app\mtsuperadmin\controller
 */
class ApplyExam extends Base
{
    /**
     * 合作申请信息列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function applyList(){
        $examStatus = $this->request->get("examStatus");
        if($examStatus){
            $where = "status = ".$examStatus;
        }else{
            $where = "1=1";
        }
        $p = $this->request->get("p") ? $this->request->get("p") : 1;
        $limit = $this->request->get("limit") ? $this->request->get("limit") : config("apiadmin.ADMIN_LIST_DEFAULT");
        $list = (new MtShopApplyExam())->where($where)->page($p,$limit)->select();
        if(!$list){
            return $this->error(ReturnCode::DB_READ_ERROR,"查询失败");
        }
        return $this->success("查询成功",$list);
    }

    /**
     * 合作审核状态修改
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function applyExamChange(){
        $id = intval($this->request->get("id"));
        $examState = intval($this->request->get("examState"));
        if(!$examState || !$id){
            return $this->error(ReturnCode::EMPTY_PARAMS,"缺少参数");
        }
        $examRemark = $this->request->get("examRemark") ? $this->request->get("examRemark") : "";
        $res = (new MtShopApplyExam())->update(["examState"=>$examState,"examRemark"=>$examRemark,"mTime"=>time()],["id"=>$id]);
        if(!$res){
            return $this->error(ReturnCode::DB_SAVE_ERROR,"ShopApplyExam修改失败");
        }
        if($examState == 2){
            $examData = (new MtShopApplyExam())->where("examState",2)->where("id",$id)->find();
            if(!$examData){
                return $this->error(ReturnCode::DB_READ_ERROR,"ApplyExam查找失败");
            }
            $insertData = [
                "shopName" => $examData['shopName'],
                "shopTel" => $examData['userTel'],
                "shopAdressInfo" => $examData['shopArea'],
                "userID" => $examData['userID'],
                "cTime" => time(),
            ];
            $res = (new MtAdminSysShopExam())->insert($insertData,true); // 转入商户信息审核表中
            if(!$res){
                return $this->error(ReturnCode::DB_SAVE_ERROR,"Shop新增失败");
            }
        }
        return $this->success("修改成功");
    }

    /**
     * 商户信息审核
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function shopExam(){
        $shopID = intval($this->request->get("shopID"));
        $examState = intval($this->request->get("examState"));
        $examOpinion = $this->request->get("examOpinion") ? $this->request->get("examOpinion") : "";
        $upRes = (new MtAdminSysShopExam())->update(["examState"=>$examState,"examOpinion"=>$examOpinion],["shopID"=>$shopID]);
        if(!$upRes){
            return $this->error(ReturnCode::DB_SAVE_ERROR,"ShopExam修改失败");
        }
        if($examState == 2){
            $shopData = (new MtAdminSysShopExam())->where("shopID",$shopID)->where("examState","2")->find();
            if(!$shopData){
                return $this->error(ReturnCode::DB_READ_ERROR,"shopExam查询失败");
            }
            $shopData = $shopData->toArray();
            unset($shopData['examState']);
            unset($shopData['examOpinion']);
            $res = (new MtShop())->insert($shopData,true);
            if(!$res){
                return $this->error(ReturnCode::DB_SAVE_ERROR,"shop新增失败");
            }
        }
        return $this->success("修改成功");
    }
}